package com.qymerge.manage.core.bean;

import lombok.Data;

import java.util.*;

/**
 * Created by chen.qiu on 2017/4/5.
 */
@Data
public class DataTableRO<T> implements java.io.Serializable {

    private int page_size;
    private int all_count;
    private int all_page;
    private int current_page;
    private Map extra;
    private List<T> list = new LinkedList<T>();

    public Map getExtra() {
        if (extra != null && extra.size() > 0) {
            return extra;
        }
        Map extraMap = new LinkedHashMap();
        extraMap.put("page_size", page_size);
        extraMap.put("all_count", all_count);
        extraMap.put("all_page", all_page);
        extraMap.put("current_page", current_page);
        return extraMap;
    }

    public static Map getDefaultExtra(int page_size, int all_count) {
        Map extraMap = new LinkedHashMap();
        extraMap.put("page_size", page_size);
        extraMap.put("all_count", all_count);
        extraMap.put("all_page", 1);
        extraMap.put("current_page", 1);
        return extraMap;
    }

    public Map build() {
        String dataKey = "list";
        return build(dataKey);
    }

    public Map build(String dataKey) {
        if (dataKey == null) {
            return null;
        }
        Map result = new LinkedHashMap();
        result.put(dataKey, this.getList());
        result.put("extra", this.getExtra());
        return result;
    }

    public Map build(AggregateDTO aggregate) {
        Map result = new HashMap();
        result.put("list", this.getList());
        result.put("extra", this.getExtra());
        if (aggregate != null) {
            result.put("aggregate", aggregate);
        }
        return result;
    }

    public DataTableRO() {

    }

    public DataTableRO(List<T> list) {
        this.list = list;
    }

    public DataTableRO(List<T> list, Map extra) {
        this.list = list;
        this.extra = extra;
    }

    public DataTableRO(int page_size, int all_count, int all_page, int current_page, List<T> list) {
        this.page_size = page_size;
        this.all_count = all_count;
        this.all_page = all_page;
        this.current_page = current_page;
        this.list = list;
    }
}
